package edu.towson.cosc.cosc455.interfacesOIMANI1markdowncompiler;

import edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class MyLexicalAnalyzer implements LexicalAnalyzer {
	
    private String[] tokens = {"#start documents", "#end document", "#start head", "#end head",
            "#start paragraph", "#end paragraph", "#start bold", "#end bold", "#start italics", "#end italics", "#start list",
            "#end list", "#start item", "#end item", "#newline", "#start audio", "#end audio", "start define",
            "#end define", "#name", "#value", "#start use", "#end use"};
    private static ArrayList<String> lexims = new ArrayList<String>();
    List<String> myValidList = Arrays.asList(tokens);
    private static String content;
    private int position;
    private String token;
    private StringBuilder stringtoToken;
    private char nextCharacter;
    
    public void MyLexicalAnalyzerStarter(String file) throws IOException{ 	
    	StringBuilder contentBuilder = new StringBuilder();
    	try {
    	    BufferedReader in = new BufferedReader(new FileReader(file));
    	    String str;
    	    while ((str = in.readLine()) != null) {
    	        contentBuilder.append(str);
    	    }
    	    in.close();
    	} catch (IOException e) {
    		System.out.println("Your File Was Not Found");
    		System.exit(0);
    	}
    	content = contentBuilder.toString();
    	stringtoToken = new StringBuilder();
		position = 0;
		
		//add the first character to the token
		getCharacter();
		addCharacter();
		
		// Continue gathering characters for the token
		int i=0;
		while (i < content.length()){
			// Checking if it is a tag
		if(nextCharacter == '#'){
	     while(!isSpace(nextCharacter)){
			addCharacter();
			getCharacter();
	     }
	     // to check if it is #newline, #name, or #value tag
	        if (lookupToken()){
			     lexims.add(token);
	           }
	          // continues to collect since its not the possible 3 tags named in previous if statement
	          else {
	    	         addCharacter();
	    	         getCharacter();
	              while(!isSpace(nextCharacter) && nextCharacter != '#'){
	        	       addCharacter();
	                   getCharacter();	        	
	                }
	        if (lookupToken()){
			   lexims.add(token);
	        }
	        else {
	    		String message = (" Lexical Error: Token not Recognized " +
	            token);
	            try {
	    			throw new CompilerException(message);
	    		    } catch (CompilerException e) {
	    			// TODO Auto-generated catch block
	    			e.printStackTrace();
	    		   }
	             }
	          }
		   }
		else {
			// Since it is not a tag then it is just text
		while(nextCharacter != '#'){
			addCharacter();
			getCharacter();
		  }
		  lexims.add(token);
		  
		}
		i++;
	  }	
    }

	@Override
	public void getNextToken() {
		// TODO Auto-generated method stub
		// Getting the next element of the array.
		for (int next = 0; next < lexims.size(); next++){
		String newToken = lexims.get(next);
		// Set the new token 
		MyCompilerClass.currentToken = newToken;
		}
	}

	@Override
	public void getCharacter() {
		// TODO Auto-generated method stub
		if (position < content.length())
			nextCharacter = content.charAt(position++);	
	}

	@Override
	public void addCharacter() {  
		//TODO Auto-generated method stub
	      stringtoToken.append(nextCharacter);
	      token = stringtoToken.toString();			
		}

    @Override
	public boolean isSpace(char c) {
		// TODO Auto-generated method stub
		if ((c == ' ')) return true;
		else return false;
	}

	@Override
	public boolean lookupToken() {
		// TODO Auto-generated method stub
		if(myValidList.contains(token))
			return true;
		else
		return false;
	}

}
